unit ControlDesign;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  IniFiles, CodeSiteLogging;

Type
  TControlDesign = Class(TComponent)
    private
      fMemIniFile:TMemIniFile;

      procedure ParseCoordinate(Buff:String; var ax,ay, aWidth, aHeight:integer);

      procedure Do_Process(ini: TCustomIniFile; aForm:TCustomForm);
    public
      constructor Create(AOwner:TComponent); override;
      Destructor  Destroy; override;
      procedure Execute(FileName: string; aForm: TCustomForm); overload;
      procedure Execute(Stream:TStream; aForm: TCustomForm); overload;
  End;


implementation

{ TControlDesign }

constructor TControlDesign.Create(AOwner: TComponent);
begin
  inherited;
end;

destructor TControlDesign.Destroy;
begin

  inherited;
end;

procedure TControlDesign.Do_Process(ini: TCustomIniFile; aForm:TCustomForm);
var
  I: Integer;
  fObjList:TStringList;
  fObjName, Buff:String;
  ax,ay,aWidth, aHeight: integer;
  obj: TComponent;
begin
  fObjList := TStringList.Create;
  try
    aWidth  := Ini.ReadInteger('Forms', AForm.ClassName +'.width', -99);
    aHeight := Ini.ReadInteger('Forms', AForm.ClassName +'.Height', -99);
    if aWidth <> -99 then TForm(aForm).Width := aWidth;
    if aHeight <> -99 then TForm(aForm).Height := aHeight;

    aWidth  := Ini.ReadInteger('Forms', AForm.ClassName +'.ClientWidth', -99);
    aHeight := Ini.ReadInteger('Forms', AForm.ClassName +'.ClientHeight', -99);
    if aWidth <> -99 then TForm(aForm).ClientWidth := aWidth;
    if aHeight <> -99 then TForm(aForm).ClientHeight := aHeight;

    ini.ReadSectionValues(aForm.ClassName, fObjList );
    for I := 0 to fObjList.Count - 1 do begin
      fObjName := fObjList.Names[i];
      Buff     := fObjList.Values[ fObjName ] ;

      ParseCoordinate(Buff, ax, ay, aWidth, aHeight);

      obj := AForm.FindComponent( fObjName );
      if obj <> nil then begin
        TControl(Obj).Left := ax;
        TControl(obj).Top  := ay;
        TControl(Obj).Width := aWidth;
        TControl(obj).Height := aHeight;
      end;
    end;
  finally
    fObjList.Free;
  end;
end;

procedure TControlDesign.Execute(FileName: string; AForm:TCustomForm);
var
  INI: TIniFile;
begin
  INI := TINIFile.Create(FileName);
  try
    Do_Process(INI, AForm);
  finally
    INI.Free;
  end;
end;

procedure TControlDesign.Execute(Stream: TStream; aForm:TCustomForm);
var
  list:TStringList;
begin

  fMemIniFile := TMemIniFile.Create('');
  FMeminiFile.CaseSensitive := False;
  List := TStringList.Create;
  try
    Stream.Position := 0;
    list.LoadFromStream( Stream );
    fMemIniFile.SetStrings( List );

    Do_Process(fMemIniFile, AForm);
  finally
    List.Free;
  end;
end;

procedure TControlDesign.ParseCoordinate(Buff: String; var ax, ay, aWidth,
  aHeight: integer);
var
  I, tmpVal: Integer;
  list:TStringList;
begin
  list := TStringLIst.Create;
  try
    list.Delimiter := ',';
    list.DelimitedText := Buff;
    for I := 0 to List.Count - 1 do Begin
      TmpVal := StrToint( Trim(list.Strings[i]) );

      case i of
        0: ax := TmpVal;
        1: ay := TmpVal;
        2: aWidth := TmpVal;
        3: aHeight := TmpVal;
      end;
    End;
  finally
    list.free;
  end;

end;

end.
